<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class adcontroller extends Controller
{
    public function index(Request $request)
    {

    	$where = [];

    	$list = DB::table('high');

    	if($request->has('name')) {

    		$where['name'] = $request->input('name');

    		$list->where('name','like',"%{$request->input('name')}%");

    	}

    	if($request->has('sex')) {

    		$where['sex'] = $request->input('sex');

    		$list->where('sex',$request->input('sex'));

    	}

    	$list = $list->paginate(5);

    	return view('admin.index',['list'=>$list,'where'=>$where]);

    }

    public function create() 
    {

    	return view('admin.add');

    }

    public function store(Request $request)
    {

        $message = [

            'required' => ':attribute 的字段是必须的',

            'min' => ':attribute 字段的值必须指定值',

        ];

        $this->validate($request,[

                'name' => 'required|unique:high',

                'password' => 'required',

                'age' => 'required|min:18|numeric',

                $message,

            ]);

    	$request = $request->only('name','password','sex','age');

    	$row = DB::table('high')->insertGetid($request);

    	if($row > 0) {

    		return redirect('/ad');

    	}

    }

    public function login() 
    {

        return view('admin.login');

    }
}
